﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * CLR版本：4.0.30319.42000
 * 文件名：Kernel
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/2/9 10:26:27
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageK.Java
{
    /// <summary>
    /// java wrapper
    /// </summary>
    public class ColorModel: Transparency
    {
        /**
         * The total number of bits in the pixel.
         */
        protected int pixel_bits;
        protected int[] nBits;
        protected int transparency = Transparency.TRANSLUCENT;
        protected bool supportsAlpha = true;
        protected bool isAlphaPremultiplied = false;
        protected int numComponents = -1;

        public int Bits { get; set; }
        public int Size { get; set; }
        public byte[] R { get; set; }
        public byte[] G { get; set; }
        public byte[] B { get; set; }
        private int _step = 1;

        public ColorModel()
        {

        }

        public ColorModel(int bits, int size, byte[] r, byte[] g, byte[] b)
        {
            this.pixel_bits = pixel_bits;
            Bits = bits;
            Size = size;
            R = new byte[size];
            G = new byte[size];
            B = new byte[size];
            Array.Copy(r,R,size);
            Array.Copy(g,G,size);
            Array.Copy(b,B,size);
            _step = 256 / size;
        }


        public byte GetR(byte val)
        {
            throw new NotImplementedException();
        }

        public byte GetG(byte val)
        {
            throw new NotImplementedException();
        }

        public byte GetB(byte val)
        {
            throw new NotImplementedException();
        }

        public void UpdatePalette(ColorPalette palette)
        {
            //灰度图像
            // for (int i = 0; i < 256; i++)
            // {
            //     palette.Entries[i] = Color.FromArgb(i, i, i);
            // }
            for (int i = 0; i < 256; i++)
            {
                var index = i / _step;
                palette.Entries[i] = Color.FromArgb(R[index], G[index], B[index]);
            }
        }

        /**
         * Returns the transparency.  Returns either OPAQUE, BITMASK,
         * or TRANSLUCENT.
         * @return the transparency of this {@code ColorModel}.
         * @see Transparency#OPAQUE
         * @see Transparency#BITMASK
         * @see Transparency#TRANSLUCENT
         */
        public override int getTransparency()
        {
            return transparency;
        }

        private static ColorModel RGBdefault;
        /**
         * Returns a {@code DirectColorModel} that describes the default
         * format for integer RGB values used in many of the methods in the
         * AWT image interfaces for the convenience of the programmer.
         * The color space is the default {@link ColorSpace}, sRGB.
         * The format for the RGB values is an integer with 8 bits
         * each of alpha, red, green, and blue color components ordered
         * correspondingly from the most significant byte to the least
         * significant byte, as in:  0xAARRGGBB.  Color components are
         * not premultiplied by the alpha component.  This format does not
         * necessarily represent the native or the most efficient
         * {@code ColorModel} for a particular device or for all images.
         * It is merely used as a common color model format.
         * @return a {@code DirectColorModel} object describing default
         *          RGB values.
         */
        public static ColorModel getRGBdefault()
        {
            if (RGBdefault == null)
            {
                RGBdefault = new DirectColorModel(32,
                    0x00ff0000,       // Red
                    0x0000ff00,       // Green
                    0x000000ff,       // Blue
                    unchecked((int)0xff000000)        // Alpha
                );
            }
            return RGBdefault;
        }
    }
}
